//
//  LoginContainerView.m
//  MVCDemo
//
//  Created by Scarecrow on 16/4/16.
//  Copyright © 2016年 XB. All rights reserved.
//

#import "TheLoginView.h"
#import "UIAlertView+show.h"
@interface TheLoginView()<UITextFieldDelegate>
@property (nonatomic,weak) id delegate;

@end
@implementation TheLoginView
{
    UIButton* RememberBtn;
    BOOL saveAccount;
    BOOL saveAccount1;
    UIButton*agreementBtn;
    UIButton*visibleBtn;
    UIButton* login;
}
@synthesize  mDelegate;
#pragma mark LifeCycle
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initUI];
    }
    return self;
}
#pragma mark InitUI
- (void)initUI
{
    UIView* myToolBar=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, viewheight)];
    myToolBar.backgroundColor=jianpancolor;
    UIButton*  tubtnDone=[[UIButton alloc]initWithFrame:CGRectMake(logoX,logoY, logoheight, logowidth)];
    [tubtnDone setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [tubtnDone addTarget:self action:@selector(OnTapDone) forControlEvents:UIControlEventTouchUpInside];
    [myToolBar addSubview:tubtnDone];
    UIButton*btnDone111=[[UIButton alloc]initWithFrame:CGRectMake(myToolBar.frame.size.width - 55, 12, 40, 16)];
    [btnDone111 setTitle:@"确定" forState:UIControlStateNormal];
    [btnDone111 setTitleColor:jianpancolor1 forState:UIControlStateNormal];
    btnDone111 .titleLabel .font=[UIFont systemFontOfSize:keyboardFot];
    [btnDone111 addTarget:self action:@selector(OnTapDone) forControlEvents:UIControlEventTouchUpInside];
    [myToolBar addSubview:btnDone111];
    myToolBar.alpha=alp;
    UIButton* dabtn=[[UIButton alloc]initWithFrame:CGRectMake(myToolBar.frame.size.width-60, 0, 60, 40)];
    [dabtn addTarget:self action:@selector(OnTapDone) forControlEvents:UIControlEventTouchUpInside];
    [myToolBar addSubview:dabtn];
    self.backgroundColor = [UIColor whiteColor];

    _userNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(20,60/2+64, ScreenWidth-40,44)];
    [_userNameTextField setBorderStyle:   UITextBorderStyleNone]; //外框类型
    _userNameTextField.placeholder = @"请输入登录账号"; //默认显示的字
    _userNameTextField.font = [UIFont systemFontOfSize: 14];
    _userNameTextField.textColor= [UIColor blackColor];
    _userNameTextField.tag=22;
    [[UIColor whiteColor] setFill];
    _userNameTextField.keyboardType = UIKeyboardTypeDecimalPad;
    _userNameTextField.returnKeyType = UIReturnKeyDone;
    _userNameTextField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
    _userNameTextField.delegate = self;
    [_userNameTextField addTarget:self action:@selector(userNumberLimitTextLength:) forControlEvents:UIControlEventEditingChanged];
    [self addSubview:_userNameTextField];
    
    _passwordTextField = [[UITextField alloc] initWithFrame:CGRectMake(20,150/2+64, ScreenWidth-40, 44)];
    [_passwordTextField setBorderStyle:   UITextBorderStyleNone]; //外框类型
    _passwordTextField.placeholder = @"请输入登录密码"; //默认显示的字
    _passwordTextField.font = [UIFont systemFontOfSize: 14];
    _passwordTextField.textColor= [UIColor blackColor];
    _passwordTextField.secureTextEntry = YES; //密码
    _passwordTextField.autocorrectionType = UITextAutocorrectionTypeNo;
    _passwordTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    _passwordTextField.returnKeyType = UIReturnKeyDone;
    [_passwordTextField addTarget:self action:@selector(passwordTextFieldLength:) forControlEvents:UIControlEventEditingChanged];
    _passwordTextField.delegate = self;
    [self addSubview:_passwordTextField];

    _passwordTextField.inputAccessoryView = myToolBar;
    _userNameTextField.inputAccessoryView = myToolBar;
    
    
    visibleBtn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth-45, 145-60+64, 25,25)];
    [visibleBtn setImage:[UIImage imageNamed:@"yanjing_normals"] forState:UIControlStateNormal];
    [visibleBtn addTarget:self action:@selector(visibleBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:visibleBtn];
    
    
    
    
    
    RememberBtn = [[UIButton alloc] initWithFrame:CGRectMake(18, 135+64, 25, 25)];
    NSUserDefaults *userDefault=[NSUserDefaults standardUserDefaults];
    NSString* UserName = [userDefault objectForKey:@"userNameTextField"];
    NSString* Password = [userDefault objectForKey:@"passwordTextField"];
    if ([userDefault objectForKey:@"SAVEACCOUNT"]) {
        [RememberBtn setImage:[UIImage imageNamed:@"jizhumima_hight"] forState:UIControlStateNormal];
        
        _userNameTextField.text=UserName;
        _passwordTextField.text=Password;
        saveAccount=[[userDefault objectForKey:@"SAVEACCOUNT"] boolValue];
        
    }
    else
    {
        [RememberBtn setImage:[UIImage imageNamed:@"jizhumima_normal"] forState:UIControlStateNormal];
        
        _userNameTextField.text=nil;
        _passwordTextField.text=nil;
        saveAccount=[[userDefault objectForKey:@"SAVEACCOUNT"] boolValue];
    }
    
    [RememberBtn addTarget:self action:@selector(RememberBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:RememberBtn];
    
    
    
    UIButton*RememberBtnbig = [[UIButton alloc] initWithFrame:CGRectMake(10, 140+64, 44, 44)];
    [RememberBtnbig addTarget:self action:@selector(RememberBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:RememberBtnbig];
    UILabel* rememberThePassword=[[UILabel alloc]initWithFrame:CGRectMake(RememberBtn.right+5, 140+64, 120.0f, 14)];
    rememberThePassword.text=@"记住密码";
    rememberThePassword.font = [UIFont systemFontOfSize: 14];
    [rememberThePassword setTextColor:zhonghuiColor];
    [self addSubview:rememberThePassword];
    
    UIButton* forget = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth-80, 140+64, 100.0f, 14)];
    [forget setTitle:@"忘记密码" forState:UIControlStateNormal];
    forget.titleLabel.textAlignment=NSTextAlignmentRight;
    forget.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
    [forget setTitleColor: luseColor forState:UIControlStateNormal];
    forget.titleLabel.font = [UIFont systemFontOfSize: 14];
  [forget addTarget:self action:@selector(forget:) forControlEvents:UIControlEventTouchUpInside];
   [self addSubview:forget];
    
    
    UILabel *line1 = [[UILabel alloc]initWithFrame:CGRectMake(10,148/2+64, ScreenWidth-20, 0.25)];
    line1.backgroundColor = qianhuiColor;
    [self addSubview:line1];
    UILabel *line2 = [[UILabel alloc]initWithFrame:CGRectMake(10,236/2+64, ScreenWidth-20, 0.25)];
    line2.backgroundColor = qianhuiColor;
    [self addSubview:line2];
    
    [self loginBtn];
    

    
    UIButton* registed = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth/2-40,540/2+64 ,80, 40)];
    [registed setTitle:@"立即注册" forState:UIControlStateNormal];
    [registed setTitleColor:luseColor forState:UIControlStateNormal];
    registed.titleLabel.font=[UIFont systemFontOfSize:16];
    [registed addTarget:self action:@selector(registed:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:registed];
    
//
    
//    NSString *str1=[NSString stringWithFormat:@"%d",TARGET_VERSION];
//    
//        if ([str1 isEqualToString:@"6"]||[str1 isEqualToString:@"16"]) {
//    
//   
//        }else{
//            UIButton*demo = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth/2-40, ScreenHeight-150+64, 65, 15)];
//            [demo setTitle:@"查看演示" forState:UIControlStateNormal];
//            [demo setTitleColor:luseColor forState:UIControlStateNormal];
//            [demo addTarget:self action:@selector(demo) forControlEvents:UIControlEventTouchUpInside];
//            demo.titleLabel.textAlignment=NSTextAlignmentCenter;
//            demo.titleLabel.font=[UIFont systemFontOfSize:15];
//            [self addSubview:demo];
//            
//            
//            
//            UIButton*jiantou = [[UIButton alloc]init];
//            jiantou.frame = CGRectMake(demo.right,ScreenHeight-149+54, 33, 33);
//            [jiantou setImage:[UIImage imageNamed:@"shouqi"] forState:UIControlStateNormal];
//            [jiantou addTarget:self action:@selector(demo) forControlEvents:UIControlEventTouchUpInside];
//            [self addSubview:jiantou];
//
//        
//    }
  

}
-(void)visibleBtn:(id)sender
{
    saveAccount1=!saveAccount1;
    if (saveAccount1) {
        UIImage *img=[UIImage imageNamed:@"yanjing_hights"];
        
        UIImage *tinted = [img rt_tintedImageWithColor:luseColor ];
        
        [visibleBtn setImage:tinted forState:UIControlStateNormal];
        _passwordTextField.secureTextEntry = NO; //密码
    }
    else
    {
        [visibleBtn setImage:[UIImage imageNamed:@"yanjing_normals"] forState:UIControlStateNormal];
        _passwordTextField.secureTextEntry =YES; //密码
    }
    
}

-(void)RememberBtn:(id)sender
{
    saveAccount=!saveAccount;
    if (saveAccount) {
            NSUserDefaults *userDefault=[NSUserDefaults standardUserDefaults];
            [userDefault setBool:saveAccount forKey:@"SAVEACCOUNT"];
            
            [userDefault setObject:_passwordTextField.text forKey:@"passwordTextField"];
            
            [userDefault synchronize];
  
       
        [RememberBtn setImage:[UIImage imageNamed:@"jizhumima_hight"] forState:UIControlStateNormal];
        
    }
    else
    {
        [RememberBtn setImage:[UIImage imageNamed:@"jizhumima_normal"] forState:UIControlStateNormal];
      
            
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"passwordTextField"];
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"SAVEACCOUNT"];
            [[NSUserDefaults standardUserDefaults] synchronize];
       

    }
    
}

#pragma mark Event Handle

- (void)demo
{
    
   
    [mDelegate userName:@"13999999999" pwd:@"reabam"];
}

- (void)login
{
     [mDelegate userName:_userNameTextField.text pwd:_passwordTextField.text];
           }
- (void)userNumberLimitTextLength:(UITextField*)textField
{
   
    
    if ([textField.text length] > 11)
    {
        textField.text = [textField.text substringToIndex:11];
    }
}
- (void)passwordTextFieldLength:(UITextField*)textField
{
    if ([textField.text length] > 2)
    {
        [self loginBtn];
    }
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    [textField resignFirstResponder];
    if (textField.tag ==22){
        
     [self loginBtn];
    }
    return YES;
}

-(void)loginBtn{
    [login removeFromSuperview];
    login = [[UIButton alloc] initWithFrame:CGRectMake(10, 525/2, ScreenWidth-20, anniuHeigth)];
    [login setTitle:@"登录" forState:UIControlStateNormal];
    [login setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [login.layer setCornerRadius:3.0]; //设置矩圆角半径
    [login.layer setBorderWidth:0.3];   //边框宽度
    CGColorSpaceRef tureBTnSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef tureBTncolorref = CGColorCreate(tureBTnSpace,(CGFloat[]){228/255.0, 228/255.0, 228/255.0, 1.0});
    [login.layer setBorderColor:tureBTncolorref];//边框颜色
    login.backgroundColor=qianhuiColor;
    [self addSubview:login];

if (_passwordTextField.text.length >1) {

   login.backgroundColor=luseColor;
    [login.layer setCornerRadius:3.0]; //设置矩圆角半径
    [login.layer setBorderWidth:0.3];   //边框宽度
    CGColorSpaceRef tureBTnSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef tureBTncolorref = CGColorCreate(tureBTnSpace,(CGFloat[]){23/255.0, 148/255.0, 152/255.0, 1.0});
    [login.layer setBorderColor:tureBTncolorref];//边框颜色
    [login addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:login];
}
}
-(void)OnTapDone{
    [_userNameTextField resignFirstResponder];
    [_passwordTextField resignFirstResponder];
    [self loginBtn];
    
    
    
}
-(void)forget:(UIButton*)sender{
    
    
    [mDelegate forgotPassword:@""];
}

-(void)registed:(UIButton*)sender{
    
  
    [mDelegate registed: @""];
    
}

//回收键盘
-(void)closeKeyBoad
{
    [_userNameTextField resignFirstResponder];
    [_passwordTextField resignFirstResponder];
    [self loginBtn];
    
}

@end
